﻿using System.Windows;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;

namespace SHomeWorkshop.LunarPresentation.Adorners
{
    /// <summary>
    /// 叶面板的折叠标记。
    /// </summary>
    public class LeafAdorner : Adorner
    {
        /// <summary>
        /// 叶面板的折叠标记。
        /// </summary>
        /// <param name="adornedElement">应传入叶面板。</param>
        public LeafAdorner(Expressions.LeafPanel adornedElement)
            : base(adornedElement)
        {
            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);

            this.masterLeaf = adornedElement;

            this.Cursor = Cursors.Hand;
            this.ToolTip = LeafAdorner.CustomToolTip;

            this.MouseDown += new MouseButtonEventHandler(LeafAdorner_MouseDown);
            this.MouseEnter += new MouseEventHandler(LeafAdorner_MouseEnter);
            this.MouseLeave += new MouseEventHandler(LeafAdorner_MouseLeave);
        }

        void LeafAdorner_MouseLeave(object sender, MouseEventArgs e)
        {
            this.InvalidateVisual();
        }

        void LeafAdorner_MouseEnter(object sender, MouseEventArgs e)
        {
            this.InvalidateVisual();
        }

        void LeafAdorner_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (masterLeaf == null) return;
            if (masterLeaf.Editor == null) return;

            masterLeaf.Editor.SelectedExpression = masterLeaf;
            e.Handled = true;
        }

        public static readonly string CustomToolTip = "被折叠，按 Z 键展开";

        private Expressions.LeafPanel masterLeaf;

        /// <summary>
        /// 使用本折叠标记的叶面板。
        /// </summary>
        public Expressions.LeafPanel MasterLeaf
        {
            get { return masterLeaf; }
            set { masterLeaf = value; }
        }

        static LeafAdorner()
        {
            transPen = new Pen(Brushes.Transparent, 1);
        }

        private static Pen transPen;

        protected override void OnRender(DrawingContext drawingContext)
        {
            if (MasterLeaf == null) return;

            Rect rect = new Rect(this.AdornedElement.RenderSize);

            Brush renderBrush = masterLeaf.ExForeground;
            Pen renderPen = new Pen(renderBrush, 1) { EndLineCap = PenLineCap.Square };

            Rect markRect;

            switch (masterLeaf.LeafType)
            {
                case Expressions.LeafPanel.LeafPanelType.Right:
                    {
                        markRect = new Rect(rect.Right + 4, rect.Top + (rect.Height / 2) - 5, 10, 10);
                        break;
                    }
                case Expressions.LeafPanel.LeafPanelType.Left:
                    {
                        markRect = new Rect(rect.Left - 13, rect.Top + (rect.Height / 2) - 5, 10, 10);
                        break;
                    }
                default: return;
            }

            if (IsMouseOver || masterLeaf.IsCollapsed)
            {
                drawingContext.DrawRectangle(Brushes.Transparent, renderPen, markRect);
                drawingContext.DrawLine(renderPen, new Point(markRect.Left + 2, markRect.Top + 5),
                    new Point(markRect.Right - 3, markRect.Top + 5));

                drawingContext.DrawLine(renderPen, new Point(markRect.Left + 5, markRect.Top + 2),
                    new Point(markRect.Left + 5, markRect.Bottom - 3));
            }
        }
    }
}